<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="缴纳时间" prop="insuranceTime">
      <!-- <el-input v-model="dataForm.insuranceTime" placeholder="缴纳时间"></el-input> -->
      <el-date-picker
         class="width400"
          v-model="dataForm.insuranceTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
    </el-form-item>
    <el-form-item label="名称" prop="insuranceName">
      <el-input v-model="dataForm.insuranceName" placeholder="名称" class="width400"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建人" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="所属公司" prop="affiliatedCompanyId">
      <el-input v-model="dataForm.affiliatedCompanyId" placeholder="所属公司"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          insuranceInfoId: 0,
          insuranceTime: '',
          insuranceName: '',
          createUserId: '',
          affiliatedCompanyId: '',
          createTime: ''
        },
        dataRule: {
          insuranceTime: [
            { required: true, message: '缴纳时间不能为空', trigger: 'blur' }
          ],
          insuranceName: [
            { required: true, message: '名称不能为空', trigger: 'blur' },
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          // createUserId: [
          //   { required: true, message: '创建人不能为空', trigger: 'blur' }
          // ],
          // affiliatedCompanyId: [
          //   { required: true, message: '所属公司不能为空', trigger: 'blur' }
          // ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.insuranceInfoId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.insuranceInfoId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/insuranceinfo/info/${this.dataForm.insuranceInfoId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.insuranceTime = data.insuranceInfo.insuranceTime
                this.dataForm.insuranceName = data.insuranceInfo.insuranceName
                this.dataForm.createUserId = data.insuranceInfo.createUserId
                this.dataForm.affiliatedCompanyId = data.insuranceInfo.affiliatedCompanyId
                this.dataForm.createTime = data.insuranceInfo.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/insuranceinfo/${!this.dataForm.insuranceInfoId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'insuranceInfoId': this.dataForm.insuranceInfoId || undefined,
                'insuranceTime': this.dataForm.insuranceTime,
                'insuranceName': this.dataForm.insuranceName,
                'createUserId': this.dataForm.createUserId,
                'affiliatedCompanyId': this.dataForm.affiliatedCompanyId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
